package com.atguigu.staservice.service.impl;

import com.atguigu.servicebase.exceptionhandler.CustomizeException;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.atguigu.staservice.service.client.UcenterClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author hwt
 * @since 2022-10-03
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Resource
    UcenterClient ucenterClient;

    @Override
    public void countRegister(String day) {
        Map<String, Object> data = ucenterClient.countRegister(day).getData();

        Integer count = (Integer) data.get("count");

        StatisticsDaily sta = new StatisticsDaily();

        sta.setDateCalculated(day);     // 统计的对应的日期
        sta.setRegisterNum(count);      // 设置注册人数

        // 其余的数据直接随机生成
        sta.setLoginNum(RandomUtils.nextInt(100, 200));      // 登录人数
        sta.setVideoViewNum(RandomUtils.nextInt(100, 200));  // 每日播放视频数
        sta.setCourseNum(RandomUtils.nextInt(100, 200));     // 每日新增课程数

        // 添加记录前，先删除数据库中的对应记录，防止重复数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);

        baseMapper.insert(sta);
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();

        if ("login_num".equals(type) || "register_num".equals(type) ||
                "video_view_num".equals(type) || "course_num".equals(type)) {

            wrapper.select(type, "date_calculated");
            wrapper.between("date_calculated", begin, end);
            List<StatisticsDaily> statisticsDailies = baseMapper.selectList(wrapper);

            ArrayList<Integer> num = new ArrayList<>();
            ArrayList<String> date = new ArrayList<>();

            for (StatisticsDaily statisticsDaily : statisticsDailies) {
                date.add(statisticsDaily.getDateCalculated());

                switch (type) {
                    case "login_num":
                        num.add(statisticsDaily.getLoginNum());
                        break;
                    case "register_num":
                        num.add(statisticsDaily.getRegisterNum());
                        break;
                    case "video_view_num":
                        num.add(statisticsDaily.getVideoViewNum());
                        break;
                    case "course_num":
                        num.add(statisticsDaily.getCourseNum());
                        break;
                    default:
                        break;
                }
            }

            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("date",date);
            hashMap.put("num",num);

            return hashMap;

        }

        throw new CustomizeException(500, "方法参数异常");

    }

}
